package com.ficus.road.maintain.third.zh.overrun;

import com.alibaba.fastjson.JSONArray;
import com.ficus.road.maintain.core.model.Coordinate;
import com.ficus.road.maintain.core.model.zh.OverloadMonitor;
import com.ficus.road.maintain.core.model.zh.OverloadMonitorZH;
import com.ficus.road.maintain.dao.OverloadMonitorDao;
import com.ficus.road.maintain.third.zh.BaseZH;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author xiawen yang
 * @date 2022/4/15 下午5:38
 */
@Component
public class ZHOverRunMonitorInfo extends BaseZH {

    @Autowired
    private OverloadMonitorDao overloadMonitorDao;

    @PostConstruct
    void init() {
        arg0 = "ytkjJianCeBaseInfo";
        arg1 = "{}";
        errMsg = "调用中海接口获取超限超载监测点基础信息异常!";
        successMsg = "调用中海接口获取超限超载监测点基础信息成功!";
    }

    @Scheduled(cron = "0 0 12,14,16 ? * MON")
//    @Scheduled(cron = "0/5 * * * * ?", zone = "GMT+8:00")
    public void refreshOverRunMonitorInfo() {
        String monitorStr = getZHData();
        if (ObjectUtils.isEmpty(monitorStr)) {
            return;
        }
        List<OverloadMonitorZH> list = JSONArray.parseArray(monitorStr, OverloadMonitorZH.class);
        list.parallelStream().forEach(monitorZH -> {
            overloadMonitorDao.saveOrUpdate(convert2OverloadMonitor(monitorZH));
        });
    }

    private OverloadMonitor convert2OverloadMonitor(OverloadMonitorZH overloadMonitorZH) {
        OverloadMonitor overloadMonitor = new OverloadMonitor();
        String[] coordinate = overloadMonitorZH.getXy().split(",");
        if (ObjectUtils.isNotEmpty(coordinate) && coordinate.length == 2) {
            Coordinate c = Coordinate.builder().x(coordinate[0]).y(coordinate[1]).build();
            overloadMonitor.setCoordinate(c);
        }
        overloadMonitor.setId(overloadMonitorZH.getCode());
        overloadMonitor.setAddress(overloadMonitorZH.getCodedesc());
        return overloadMonitor;
    }
}
